import asyncio

from loguru import logger

from com.arcfox.base.base_spider import BaseSpider, ExecuteType
from com.arcfox.middleware.async_redis_middleware import open_redis
from com.arcfox.school.processor.yun720.yun_vr_processor import YunVRProcessor
from com.arcfox.util import async_request as requests
from com.arcfox.middleware import async_mysql_middleware as db
from com.arcfox.util.util import random_sleep


class YunVRSpider(BaseSpider):
    def __init__(self):
        self.url = "https://api-search.720yun.com/search"
        self.processor = YunVRProcessor()
        self.APP_KEY = "eByjUyLDG2KtkdhuTsw2pY46Q3ceBPdT"

    async def get_tasks(self):
        mapping = db.MysqlMapping("tb_university_info")
        result = await mapping.query({}, ['university_name', 'university_uid'])
        tasks = []
        if result:
            for item in result:
                university_name = item[0]
                university_uid = item[1]
                tasks.append({"name": university_name, "uid": university_uid})
        return tasks

    @open_redis
    async def init_data_version(self, client):
        pass

    def get_headers(self):
        return {
            'app-key': self.APP_KEY,
            'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.51 Safari/537.36',
            'origin': 'https://720yun.com',
            'referer': 'https://720yun.com/',
        }

    def get_params(self, university_name):
        return {
            'channelId': '30',
            'content': university_name,
            'page': '1',
            'selected': '2',
            'type': '1',
        }

    async def _pull_task(self):
        return await self.get_tasks(), ExecuteType.ONCE

    async def _crawl_by_task(self, tasks):
        for task in tasks:
            resp = await requests.get_with_session(self.url, params=self.get_params(task['name']),
                                                   headers=self.get_headers())
            if resp.code == 200:
                await self.processor.parse_and_save_data(task, resp.json())
            else:
                logger.warning(resp)
            await random_sleep(0.2)


if __name__ == "__main__":
    asyncio.run(YunVRSpider().start_crawl())
